package com.demo.shiro;


import com.demo.entity.SysUser;
import com.demo.service.ISysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;


public class LoginRealm extends AuthorizingRealm {


    @Autowired
    private ISysUserService userService;

    /*
    * 授权
    * */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /*
    * 认证
    * */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String email = token.getUsername();
        SysUser tbUser = userService.findUsername(email);
        if (tbUser == null){
            return null;
        }
        return new SimpleAuthenticationInfo(tbUser , tbUser.getPassword() , ByteSource.Util.bytes(tbUser.getSalt()) , tbUser.getEmail());
    }
}
